﻿﻿using System;
using StarTrek.Ship;
using StarTrek.World;

namespace StarTrek.Weapon
{
    class Repair:iWeapon
    {
        private float cHullStrength;
        private float cMaxHull;
        private const float LABOR_EFF = (float) .80;
        private bool damaged;
        private string name;
        private iShip parent;

        public Repair(float HullStrength)
        {
            cHullStrength = HullStrength;
            cMaxHull = HullStrength;
        }
        public Repair(){}

        public float HullStrength
        {
            get
            {
                return cHullStrength;
            }
        }
        public float HullPerc
        {
            get
            {
                return cHullStrength / cMaxHull;
            }
        }

        internal float DamageHull(float DamagePoints)
        {
            float est = cHullStrength - DamagePoints;
            float rVal;

            if (est > 0)
            {
                cHullStrength = est;
                rVal = 0;
            }
            else
            {
                cHullStrength = 0;
                rVal = 0 - est;
            }

            return rVal;
        }

        #region iWepon Members

        public ShipStatus ApplyWeponToShip(iShip Target, float Energy)
        {
            throw new InvalidWeaponException(this, Target);
        }

        public iShip ApplyDefenseToShip(float Energy)
        {
            float startingStrength = cHullStrength;
            //this is conceptually repairing the hull with engery(labor).
            cHullStrength += (Energy * LABOR_EFF);

            if (cHullStrength <= cMaxHull)
                cHullStrength = cMaxHull; //The Player did lose extra engery in this case.

            System.Diagnostics.Debug.WriteLine(String.Format("Used {0} units of engery to repaired the hull {1} units - {2:#.0000} effecicent", Energy, (cHullStrength - startingStrength), Energy / (cHullStrength - startingStrength)));
            return this.parent;
        }

        public iShip Parent
        {
            get
            {
                return parent;
            }
            set
            {
                parent = value;
            }
        }

        public bool isFunctional
        {
            get
            {
                return !damaged;
            }
            set
            {
                damaged = !value;
            }
        }

        #endregion

        #region iWeapon Members


        public string WeaponReport()
        {
            return String.Format("Ship hull integrity is currently at {0:###}% ", Math.Round(cHullStrength/cMaxHull, 2)*100);
        }

        #endregion

        #region iWeapon Members


        public void Load(System.Xml.XmlNode node)
        {
            //Strength="2000"
            name = node.Attributes["Name"].Value;
            cHullStrength = Convert.ToSingle(node.Attributes["Strength"].Value);
            cMaxHull = Convert.ToSingle(node.Attributes["Strength"].Value);
        }
        public bool isOffensive { get { return false; } }
        public bool NeedsEnegery { get { return true; } }
        public string Name
        {
            get { return name; }
        }
        #endregion
    }
}
